package app.itgungnir.compose.wan.business.coin_rank

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.BottomAppBar
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import app.itgungnir.compose.wan.utils.LocalDataStore
import app.itgungnir.compose.wan.widgets.WanPagingList
import app.itgungnir.compose.wan.widgets.WanScaffold
import com.google.accompanist.insets.navigationBarsPadding

@Composable
fun CoinRankScreen(navController: NavController = rememberNavController()) {
    val viewModel = viewModel<CoinRankViewModel>()
    // UI
    WanScaffold(
        modifier = Modifier.navigationBarsPadding(),
        title = "积分排行榜",
        navController = navController,
        bottomBar = {
            if (LocalDataStore.localData.hasLogin) {
                BottomAppBar(
                    modifier = Modifier.fillMaxWidth(),
                    backgroundColor = MaterialTheme.colors.background
                ) {
                    val userInfo = LocalDataStore.localData.userInfo
                    val coinInfo = LocalDataStore.localData.coinInfo
                    CoinRankItem(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 15.dp),
                        item = CoinRank(
                            userId = userInfo.id,
                            username = userInfo.name,
                            level = coinInfo.level,
                            coinCount = coinInfo.coins,
                            rank = coinInfo.rank
                        )
                    )
                }
            }
        }
    ) {
        val pagingItems = viewModel.dataFlow.collectAsLazyPagingItems()
        WanPagingList(modifier = Modifier.padding(paddingValues = it), pagingItems = pagingItems) {
            items(pagingItems) {
                CoinRankItem(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 10.dp, start = 15.dp, end = 15.dp)
                        .background(color = MaterialTheme.colors.background, shape = RoundedCornerShape(size = 12.dp))
                        .padding(horizontal = 15.dp, vertical = 10.dp),
                    item = it
                )
            }
        }
    }
}
